package communityController;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import util.FileSaveUtil;
import vo.CommunityVo;
import dao.CommunityDao;

@Controller
public class CommunityBoardModify {
	
	@Autowired
	CommunityDao cdao;
	
	@Autowired
	HttpServletRequest request; 

	@RequestMapping(value="/project/community/board_modify.do")
	public ModelAndView exe(CommunityVo cvo) throws Exception {
		
		int menu_no=cvo.getMenu_no();
		int cate_no=cvo.getCate_no();
		int title_no=cvo.getTitle_no();
		
		String ip = request.getRemoteAddr();
		String filename=cvo.getB_filename();
		MultipartFile file = cvo.getFile();
		
		if(!file.isEmpty()){
			String absPath = request.getServletContext().getRealPath("upload");
			if(!filename.equals("no_file")){
				String filePath = absPath+"/"+filename;
				new File(filePath).delete();
			}
			filename=file.getOriginalFilename();
			filename=FileSaveUtil.saveFile(file, filename, absPath);
		}
		
		cvo.setB_filename(filename);
		cvo.setB_ip(ip);
		
		cdao.modifyCommunityBoard(cvo);

		ModelAndView mv = new ModelAndView();
		mv.addObject("state", "view");
		//mv.addObject("menu_no", menu_no);
		mv.addObject("cate_no", cate_no);
		mv.addObject("title_no", title_no);
		mv.addObject("b_no",cvo.getB_no());
		mv.setViewName("redirect:/project/community/board_view.do");
		return mv;
	}

}
